﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Dryricenet
{
    public partial class home_Order : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string name = Session["Name"].ToString();
                DataTable dt = userBLL.selectNameBll(name);
                DataRow dr = dt.Rows[0];//第一条数据
                int id = Convert.ToInt32(dr["UserID"]);
                Literal1.Text = CartBLL.totalBLL(id).ToString();
                CK(id);
                Xlk();
                string OrderNo = DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0') + DateTime.Now.ToString("fff");
                Literal2.Text = OrderNo.ToString();
            }
        }
        //订单查看
        public void CK(int id)
        {
            GridView1.DataSource = CartBLL.cartCKBll(id);
            GridView1.DataBind();
        }
        //下拉框
        public void Xlk()
        {
            DropDownList1.DataSource = payBLL.liXlkBLL();
            DropDownList1.DataTextField = "PayName";
            DropDownList1.DataValueField = "PayID";
            DropDownList1.DataBind();
        }
       
        //提交订单
        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = Session["Name"].ToString();
            DataTable dt = userBLL.selectNameBll(name);
            DataRow dr = dt.Rows[0];//第一条数据
            int id = Convert.ToInt32(dr["UserID"]);
            //添加订单
            string address = txtDiZi.Text.Trim().ToString() + txtXX.Text.Trim().ToString();
            Order o = new Order();
            o.Ordertitle = Literal2.Text;
            o.OrderName = txtName.Text.Trim();
            o.Phone = txtPhone.Text.Trim();
            o.PayID = DropDownList1.SelectedIndex + 1;
            o.UserID = id;
            o.Pay = Convert.ToDouble(Literal1.Text);
            o.Address = address;
            o.Time = DateTime.Now;
            o.Note = txtBeizu.Text.Trim();
            if (txtName.Text.Trim() != "" && txtPhone.Text.Trim() != "" && txtDiZi.Text.Trim() != "" && txtXX.Text.Trim() != "")
            {
                OrderBLL.orderAdd(o);
                //添加订单详情
                int orderid = Convert.ToInt32(OrderBLL.orderIDBLL(Literal2.Text));
                OrderDetails od = new OrderDetails();
                od.OrderID = orderid;
                OrderDetailsBLL.orderDetailsAddBLL(od,id);
                CartBLL.deleteAllBLL(id);
               
                Response.Redirect("home_Pay.aspx?ordertitle=" + Literal2.Text + "&sum=" + Literal1.Text);
            }
            else
            {
                Response.Write("<script>javascript:alert('请将收件人信息填写完整!');history.back();</script>");
            }

        }
    }
}